/*   1:    */ package com.atomikos.icatch.jta;
/*   2:    */ 
/*   3:    */ import com.atomikos.icatch.CompositeTransaction;
/*   4:    */ import com.atomikos.icatch.SysException;
/*   5:    */ import com.atomikos.icatch.TransactionControl;
/*   6:    */ import com.atomikos.icatch.system.Configuration;
/*   7:    */ import java.rmi.RemoteException;
/*   8:    */ import java.rmi.server.UnicastRemoteObject;
/*   9:    */ import java.util.Enumeration;
/*  10:    */ import java.util.Hashtable;
/*  11:    */ import java.util.Properties;
/*  12:    */ import java.util.Stack;
/*  13:    */ import javax.naming.Context;
/*  14:    */ import javax.naming.InitialContext;
/*  15:    */ import javax.naming.NamingException;
/*  16:    */ import javax.rmi.PortableRemoteObject;
/*  17:    */ import javax.transaction.HeuristicMixedException;
/*  18:    */ import javax.transaction.HeuristicRollbackException;
/*  19:    */ import javax.transaction.NotSupportedException;
/*  20:    */ import javax.transaction.RollbackException;
/*  21:    */ import javax.transaction.SystemException;
/*  22:    */ import javax.transaction.Transaction;
/*  23:    */ import javax.transaction.UserTransaction;
/*  24:    */ 
/*  25:    */ public class UserTransactionServerImp
/*  26:    */   implements UserTransactionServer
/*  27:    */ {
/*  28: 64 */   private static UserTransactionServerImp singleton_ = null;
/*  29:    */   private TransactionManagerImp tm_;
/*  30:    */   private String name_;
/*  31:    */   private boolean exported_;
/*  32:    */   private String initialContextFactory_;
/*  33:    */   private String providerUrl_;
/*  34:    */   private Properties properties_;
/*  35:    */   
/*  36:    */   public static synchronized UserTransactionServerImp getSingleton()
/*  37:    */   {
/*  38: 68 */     if (singleton_ == null) {
/*  39: 69 */       singleton_ = new UserTransactionServerImp();
/*  40:    */     }
/*  41: 71 */     return singleton_;
/*  42:    */   }
/*  43:    */   
/*  44:    */   private UserTransactionServerImp()
/*  45:    */   {
/*  46: 96 */     this.name_ = "UserTransactionServer";
/*  47: 97 */     this.exported_ = false;
/*  48:    */   }
/*  49:    */   
/*  50:    */   private Context getInitialContext()
/*  51:    */     throws NamingException
/*  52:    */   {
/*  53:110 */     Hashtable env = new Hashtable();
/*  54:111 */     Enumeration enumm = this.properties_.propertyNames();
/*  55:112 */     while (enumm.hasMoreElements())
/*  56:    */     {
/*  57:113 */       String name = (String)enumm.nextElement();
/*  58:114 */       String value = this.properties_.getProperty(name);
/*  59:115 */       env.put(name, value);
/*  60:    */     }
/*  61:117 */     return new InitialContext(env);
/*  62:    */   }
/*  63:    */   
/*  64:    */   public UserTransaction getUserTransaction()
/*  65:    */   {
/*  66:129 */     UserTransaction ret = null;
/*  67:130 */     if (this.exported_) {
/*  68:131 */       ret = new RemoteClientUserTransaction(getName(), this.initialContextFactory_, this.providerUrl_);
/*  69:    */     }
/*  70:134 */     return ret;
/*  71:    */   }
/*  72:    */   
/*  73:    */   public String getName()
/*  74:    */   {
/*  75:145 */     return this.name_;
/*  76:    */   }
/*  77:    */   
/*  78:    */   public void init(String tmUniqueName, Properties properties)
/*  79:    */     throws SysException
/*  80:    */   {
/*  81:162 */     boolean canExport = false;
/*  82:163 */     String exportClass = properties.getProperty("com.atomikos.icatch.rmi_export_class");
/*  83:    */     
/*  84:165 */     canExport = ("UnicastRemoteObject".equals(exportClass)) || ("PortableRemoteObject".equals(exportClass));
/*  85:167 */     if (!canExport)
/*  86:    */     {
/*  87:168 */       Configuration.logWarning("Client transaction demarcation not supported for com.atomikos.icatch.rmi_export_class=" + exportClass);
/*  88:    */       
/*  89:    */ 
/*  90:    */ 
/*  91:172 */       this.exported_ = false;
/*  92:    */     }
/*  93:    */     else
/*  94:    */     {
/*  95:174 */       this.name_ = (tmUniqueName + "UserTransactionServer");
/*  96:175 */       this.providerUrl_ = properties.getProperty("java.naming.provider.url");
/*  97:176 */       if (this.providerUrl_ == null) {
/*  98:177 */         throw new SysException("Startup property java.naming.provider.url must be set for client demarcation.");
/*  99:    */       }
/* 100:181 */       this.initialContextFactory_ = properties.getProperty("java.naming.factory.initial");
/* 101:183 */       if (this.initialContextFactory_ == null) {
/* 102:184 */         throw new SysException("Startup property java.naming.factory.initial must be set for client demarcation.");
/* 103:    */       }
/* 104:188 */       this.properties_ = properties;
/* 105:    */       
/* 106:190 */       this.tm_ = ((TransactionManagerImp)TransactionManagerImp.getTransactionManager());
/* 107:192 */       if (this.tm_ == null) {
/* 108:193 */         throw new SysException("No TM found");
/* 109:    */       }
/* 110:    */       try
/* 111:    */       {
/* 112:197 */         if ("PortableRemoteObject".equals(exportClass))
/* 113:    */         {
/* 114:198 */           PortableRemoteObject.exportObject(this);
/* 115:199 */           this.exported_ = true;
/* 116:    */         }
/* 117:200 */         else if ("UnicastRemoteObject".equals(exportClass))
/* 118:    */         {
/* 119:201 */           UnicastRemoteObject.exportObject(this);
/* 120:202 */           this.exported_ = true;
/* 121:    */         }
/* 122:    */       }
/* 123:    */       catch (Exception e)
/* 124:    */       {
/* 125:206 */         Stack errors = new Stack();
/* 126:207 */         errors.push(e);
/* 127:208 */         throw new SysException("Error exporting - naming service not running?", errors);
/* 128:    */       }
/* 129:    */       try
/* 130:    */       {
/* 131:214 */         Context ctx = getInitialContext();
/* 132:215 */         ctx.rebind(this.name_, this);
/* 133:    */       }
/* 134:    */       catch (Exception e)
/* 135:    */       {
/* 136:217 */         Stack errors = new Stack();
/* 137:218 */         errors.push(e);
/* 138:219 */         throw new SysException("Please make sure the rmiregistry is running!?", errors);
/* 139:    */       }
/* 140:    */     }
/* 141:    */   }
/* 142:    */   
/* 143:    */   public void shutdown()
/* 144:    */     throws SysException
/* 145:    */   {
/* 146:234 */     if (this.exported_)
/* 147:    */     {
/* 148:235 */       String exportClass = this.properties_.getProperty("com.atomikos.icatch.rmi_export_class");
/* 149:    */       try
/* 150:    */       {
/* 151:238 */         if ("PortableRemoteObject".equals(exportClass)) {
/* 152:239 */           PortableRemoteObject.unexportObject(this);
/* 153:240 */         } else if ("UnicastRemoteObject".equals(exportClass)) {
/* 154:241 */           UnicastRemoteObject.unexportObject(this, true);
/* 155:    */         }
/* 156:243 */         Context ctx = getInitialContext();
/* 157:244 */         ctx.unbind(this.name_);
/* 158:    */       }
/* 159:    */       catch (Exception e)
/* 160:    */       {
/* 161:246 */         Stack errors = new Stack();
/* 162:247 */         errors.push(e);
/* 163:248 */         throw new SysException(e.getMessage(), errors);
/* 164:    */       }
/* 165:252 */       this.exported_ = false;
/* 166:    */     }
/* 167:    */   }
/* 168:    */   
/* 169:    */   public String begin(int timeout)
/* 170:    */     throws RemoteException, SystemException, NotSupportedException
/* 171:    */   {
/* 172:265 */     this.tm_.begin(timeout);
/* 173:266 */     TransactionImp tx = (TransactionImp)this.tm_.getTransaction();
/* 174:    */     
/* 175:268 */     tx.getCT().getTransactionControl().setSerial();
/* 176:269 */     this.tm_.suspend();
/* 177:270 */     return tx.getCT().getTid();
/* 178:    */   }
/* 179:    */   
/* 180:    */   public void commit(String tid)
/* 181:    */     throws RemoteException, RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException
/* 182:    */   {
/* 183:282 */     Transaction tx = this.tm_.getPreviousInstance(tid);
/* 184:283 */     if (tx == null) {
/* 185:284 */       throw new RollbackException("Transaction not found: " + tid);
/* 186:    */     }
/* 187:287 */     tx.commit();
/* 188:    */   }
/* 189:    */   
/* 190:    */   public void rollback(String tid)
/* 191:    */     throws RemoteException, IllegalStateException, SecurityException, SystemException
/* 192:    */   {
/* 193:297 */     Transaction tx = this.tm_.getPreviousInstance(tid);
/* 194:298 */     if (tx != null) {
/* 195:299 */       tx.rollback();
/* 196:    */     }
/* 197:    */   }
/* 198:    */   
/* 199:    */   public void setRollbackOnly(String tid)
/* 200:    */     throws RemoteException, IllegalStateException, SystemException
/* 201:    */   {
/* 202:310 */     Transaction tx = this.tm_.getPreviousInstance(tid);
/* 203:311 */     if (tx != null) {
/* 204:312 */       tx.setRollbackOnly();
/* 205:    */     }
/* 206:    */   }
/* 207:    */   
/* 208:    */   public int getStatus(String tid)
/* 209:    */     throws RemoteException, SystemException
/* 210:    */   {
/* 211:322 */     int ret = 6;
/* 212:323 */     Transaction tx = this.tm_.getPreviousInstance(tid);
/* 213:324 */     if (tx != null) {
/* 214:325 */       ret = tx.getStatus();
/* 215:    */     }
/* 216:327 */     return ret;
/* 217:    */   }
/* 218:    */ }


/* Location:           C:\myfile\mysoft\apache-maven-3.5.0-bin\apache-maven-3.5.0\myrep\com\atomikos\transactions-jta\3.7.0\transactions-jta-3.7.0.jar
 * Qualified Name:     com.atomikos.icatch.jta.UserTransactionServerImp
 * JD-Core Version:    0.7.0.1
 */